package com.jss.lanouhn.fristapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.HomeTitleAdapter;
import com.jss.lanouhn.fristapp.modles.HomeTitlesInfo;
import com.squareup.picasso.Picasso;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/31.
 */
public class HomeTitleActivity extends AppCompatActivity {
    private String urlHead = "http://api.liwushuo.com/v2/columns/";
    private String urlEnd = "?limit=20&offset=0";
    private String url;
    private HomeTitlesInfo titlesInfo;
    private ImageView iv_back,iv_share,iv_likes;
    private TextView tv_title,tv_likes,tv_attention,tv_introduction;
    private RecyclerView rv_titles;
    private HomeTitleAdapter HTAdapter;
    private ImageView iv_background;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_title_detail);
        initData();
        initEvent();
        forwebData();

    }



    private void initData() {
        Intent intent = getIntent();
         String urlId = intent.getStringExtra("id");

        url = urlHead + urlId + urlEnd;

        iv_background= (ImageView) findViewById(R.id.iv_background);
        iv_back= (ImageView) findViewById(R.id.iv_title);
        iv_share= (ImageView) findViewById(R.id.iv_share);
        iv_likes= (ImageView) findViewById(R.id.iv_likes);
        tv_title= (TextView) findViewById(R.id.tv_title_name);
        tv_likes= (TextView) findViewById(R.id.tv_likes);
        tv_attention= (TextView) findViewById(R.id.tv_attention);
        tv_introduction= (TextView) findViewById(R.id.tv_introduction);
        rv_titles= (RecyclerView) findViewById(R.id.rv_title);
        tv_attention= (TextView) findViewById(R.id.tv_attention);

    }

    private void forwebData() {
        OkHttpClient client = new OkHttpClient();
        Log.d("title", "" + url+"87878*&");
        Request request = new Request
                .Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Gson gson = new Gson();
                titlesInfo = gson.fromJson(result, HomeTitlesInfo.class);
                if (titlesInfo.getData().getPosts().size()>0){
                    handler.sendEmptyMessage(1);
                }else{
                    Toast.makeText(HomeTitleActivity.this, "网路解析数据错误", Toast.LENGTH_SHORT).show();
                }

            }
        });



    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Picasso.with(HomeTitleActivity.this).load(titlesInfo.getData().getBanner_image_url()).into(iv_background);
//                    x.image().bind(iv_back,);
                    tv_title.setText(titlesInfo.getData().getTitle());
                    tv_likes.setText(titlesInfo.getData().getLikes_count()+"人喜欢");
                    tv_introduction.setText(titlesInfo.getData().getDescription());
                    HTAdapter=new HomeTitleAdapter(HomeTitleActivity.this,titlesInfo);
                    rv_titles.setAdapter(HTAdapter);
                    LinearLayoutManager manager = new LinearLayoutManager(HomeTitleActivity.this);
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    rv_titles.setLayoutManager(manager);
                    break;
            }
            return true;
        }
    });

    private void initEvent() {

        tv_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(HomeTitleActivity.this,LoginActivity.class));
            }
        });
    }





}
